package link;

/**
 * @Program: dataOJ->DoubleLinkList
 * @Description: 双向链表
 * @Author: Tansty
 * @Date: 2022-02-06 21:10
 * @Version： 1.0
 **/

public class DoubleLinkList {
    private Animal2 head = new Animal2(0, "", "");

    public void add(Animal2 animal) {
        Animal2 temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = animal;
        animal.pre = temp;
    }

    public void list() {
        if (isNull()) {
            System.out.println("链表中没有内容");
            return;
        }
        Animal2 temp = head;
        while (temp.next != null) {
            temp = temp.next;
            System.out.println(temp);
        }
    }

    public boolean isNull() {
        return head.next == null;
    }

    public void update(Animal2 animal) {
        Animal2 temp = head;
        boolean flag = false;
        while (temp.next != null) {
            temp = temp.next;
            if (temp.id == animal.id) {
                flag = true;
                break;
            }
        }
        if (flag) {
            temp.name = animal.name;
            temp.nickName = animal.nickName;
            System.out.println(temp);
        } else {
            System.out.println("没有您所修改的动物");
        }

    }

    public void delete(Animal2 animal) {
        Animal2 temp = head;
        boolean flag = false;
        while (temp != null) {
            if (temp.id == animal.id) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.pre.next = temp.next;
            if (temp.next != null) {
                temp.next.pre = temp.pre;
            }

        } else {
            System.out.println("您所删除的动物不存在");
        }

    }

    public void addByOrder(Animal2 animal) {
        Animal2 temp = head;
        while (temp.next != null) {
            if (temp.next.id > animal.id) {
                animal.next = temp.next;
                break;
            }
            temp = temp.next;
        }
        temp.next = animal;
        animal.pre = temp;
    }

    public static void main(String[] args) {
        Animal2 tansty = new Animal2(1, "tansty", "🐷");
        Animal2 tansty1 = new Animal2(2, "tansty1", "💩");
        Animal2 tansty2 = new Animal2(3, "tansty", "🐼");
        DoubleLinkList doubleLinkList = new DoubleLinkList();
        doubleLinkList.addByOrder(tansty);
        doubleLinkList.addByOrder(tansty2);
        doubleLinkList.addByOrder(tansty1);
        doubleLinkList.list();
    }
}

class Animal2 {
    int id;
    String name;
    String nickName;
    Animal2 next;
    Animal2 pre;

    public Animal2(int id, String name, String nickName) {
        this.id = id;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "Animal{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}
